package cn.lyq.card.ui.message

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.MsgSysInfo
import cn.lyq.card.data.model.PageInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.manager.UserManager
import cn.lyq.card.utils.getVisibility
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_message.*
import kotlinx.android.synthetic.main.layout_title.*
import kotlinx.android.synthetic.main.layout_title.tvTitleSplit


class MessageActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(MessageVM::class.java)
    }
    private val mAdapterSys by lazy {
        MsgSysAdapter()
    }
    private val mAdapterMy by lazy {
        MsgMyAdapter()
    }
    private val pageSys = PageInfo()
    private val pageMy = PageInfo()

    override fun getLayoutId(): Int {
        return R.layout.act_message
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_message_center)
        tvTitleSplit.visibility = View.GONE
        tvTitleRight.setText(R.string.str_read_all_one_key)
        changeTab(true)

        rvMy.layoutManager = LinearLayoutManager(this)
        rvMy.adapter = mAdapterMy

        rvSys.layoutManager = LinearLayoutManager(this)
        rvSys.adapter = mAdapterSys

        mAdapterSys.loadMoreModule.apply {
            checkDisableLoadMoreIfNotFullPage()
            preLoadNumber = 2
            setOnLoadMoreListener {
                getData(pageSys.loadMore(), 2)
            }
        }
        mAdapterSys.loadMoreModule.apply {
            checkDisableLoadMoreIfNotFullPage()
            preLoadNumber = 2
            setOnLoadMoreListener {
                getData(pageMy.loadMore(), 1)
            }
        }

        tvUnReadPoint.visibility = getVisibility(UserManager.unreadNotice.value == 0)
    }

    override fun initListener() {
        super.initListener()
        tvMyMessage.setClick {
            changeTab(true)
        }
        tvSysMessage.setClick {
            changeTab(false)
        }
        refreshLayoutSys.setOnRefreshListener {
            getData(pageSys.refresh(), 2)
        }
        refreshLayoutMy.setOnRefreshListener {
            getData(pageMy.refresh(), 1)
        }
        mAdapterSys.setOnItemClickListener { _, _, position ->
            mModel.read(mAdapterSys.data[position].id)
        }
        tvTitleRight.setClick {
            mModel.readAll().observe(this, Observer {
                when (it?.status) {
                    Status.LOADING -> showLoading()
                    Status.SUCCESS -> {
                        ToastUtils.showShort("已全部标记为已读")
                        dismissLoading()
                        tvUnReadPoint.visibility = View.GONE
                        refreshLayoutSys.isRefreshing = true
                        getData(pageSys.refresh(), 2)
                    }
                    Status.ERROR -> {
                        ToastUtils.showShort(it.message.getMsg())
                        dismissLoading()
                    }
                }
            })
        }
    }

    override fun initObserver() {
        super.initObserver()
        refreshLayoutSys.isRefreshing = true
        getData(pageMy.refresh(), 1)
        getData(pageSys.refresh(), 2)
    }

    private fun changeTab(first: Boolean) {
        tvMyMessage.isSelected = first
        tvSysMessage.isSelected = !first
        tvTitleRight.visibility = getVisibility(first)
        refreshLayoutMy.visibility = getVisibility(!first)
        refreshLayoutSys.visibility = getVisibility(first)
    }

    private fun getData(page: Int, type: Int) {
        mModel.getSys(page, type).observe(this, Observer {
            when (it?.status) {
                Status.SUCCESS -> {
                    loadSuccess(type, it.requireData().list)
                }
                Status.ERROR -> {
                    if (type == 1) {
                        pageMy.fail()
                        refreshLayoutMy.isRefreshing = false
                        mAdapterMy.loadMoreModule.loadMoreFail()
                        ToastUtils.showShort(it.message.getMsg())
                    } else {
                        pageSys.fail()
                        refreshLayoutSys.isRefreshing = false
                        mAdapterSys.loadMoreModule.loadMoreFail()
                        ToastUtils.showShort(it.message.getMsg())
                    }

                }
            }
        })
    }

    private fun loadSuccess(type: Int, list: MutableList<MsgSysInfo>) {
        if (type == 1) {
            if (pageMy.isRefresh()) {
                mAdapterMy.setList(list)
            } else {
                mAdapterMy.addData(list)
            }
            pageMy.success()
            if (list.size < mModel.pageSize) {
                mAdapterMy.loadMoreModule.loadMoreEnd()
            } else {
                mAdapterMy.loadMoreModule.loadMoreComplete()
            }
            refreshLayoutMy.isRefreshing = false
        } else {
            if (pageSys.isRefresh()) {
                mAdapterSys.setList(list)
            } else {
                mAdapterSys.addData(list)
            }
            pageSys.success()
            if (list.size < mModel.pageSize) {
                mAdapterSys.loadMoreModule.loadMoreEnd()
            } else {
                mAdapterSys.loadMoreModule.loadMoreComplete()
            }
            refreshLayoutSys.isRefreshing = false
        }
    }
}